<template>
  <div class="flex flex-col h-full">
    <a-card class="flex-1">
      <div class="flex justify-between">
        <div style="width: 63%">
          <div class="flex justify-between mb-2">
            <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '70px' } }">
              <a-form-item name="pyjm" class="w-72 mb-0">
                <a-input v-model:value="formState.pyjm" placeholder="诊断代码/诊断名称/助记码" allow-clear @press-enter="getData()" @blur="getData()" />
              </a-form-item>
              <a-form-item name="nmiStatus" class="w-72 mb-0 ml-8">
                <a-radio-group v-model:value="formState.nmiStatus" @change="getData()">
                  <a-radio value="">全部</a-radio>
                  <a-radio value="0">未对照</a-radio>
                  <a-radio value="1">已对照</a-radio>
                </a-radio-group>
              </a-form-item>
            </a-form>
            <div class="flex justify-between">
              <BaseExport type="医保" name="临床诊断对照" code="001058" :search-params="formState">导出</BaseExport>
              <a-button class="ml-2" @click="handleReset">重置</a-button>
              <a-button class="ml-2 mr-2" type="primary" @click="getData()">搜索</a-button>
              <a-button type="primary" @click="autoCompare">自动对照</a-button>
            </div>
          </div>
          <BaseTable
            column-code="001058"
            fill
            :data-source="tableData"
            bordered
            :scroll="{ x: 1600 }"
            :custom-row="(record) => ({ onClick: () => onSelectRow(record) })"
            :row-class-name="(record) => (record.zddm === selectLeftRow.zddm ? 'selected-row' : '')"
            :pagination="{ total: page.total, current: page.current, pageSize: page.pageSize }"
            @change="onPageChange"
          >
            <template #bodyCell="{ column, index, record }">
              <template v-if="column.dataIndex === 'xh'">
                {{ index + 1 }}
              </template>
              <template v-if="column.dataIndex === 'nmiStatus'">
                <a-tag v-if="record.nmiStatus === '未对照'" color="blue">未对照</a-tag>
                <a-tag v-if="record.nmiStatus === '已对照'" color="green">已对照</a-tag>
              </template>
              <template v-if="column.dataIndex === 'ztflag'">
                <a-switch v-model:checked="record.ztflag" :checked-value="0" :un-checked-value="1" @change="onHandleDeactivate(record.zddm, $event)" />
              </template>
              <!-- <template v-if="column.dataIndex === 'insureFlag'">
                <a-switch v-model:checked="record.insureFlag" :un-checked-value="0" :checked-value="1" @change="onChangeFlag(record)" />
              </template> -->
              <template v-if="column.dataIndex === 'nmiIcd10Code'">
                <EditCell title="国家医保诊断编码">
                  <BaseInput v-model:value="record.nmiIcd10Code" @change="record.change = true" @blur="onBlurInput(record)" />
                </EditCell>
              </template>
              <!-- <template v-if="column.dataIndex === 'nmiIcd10Name'">
                <EditCell title="国家医保诊断名称	">
                  <BaseInput v-model:value="record.nmiIcd10Name" @change="record.change = true" @blur="onBlurInput(record)" />
                </EditCell>
              </template> -->
              <template v-if="column.dataIndex === 'action'">
                <BaseButton confirm-text="确认撤销?" type="link" danger size="small" :disabled="record.nmiStatus === '未对照'" @click="cancelCompare(record)">
                  撤销
                </BaseButton>
              </template>
            </template>
          </BaseTable>
          <div class="flex justify-between">
            <p :class="[tableData.length > 0 ? 'py-0 my-0 ant-tag-red font-bold -mt-8 ' : 'py-0 my-0 ant-tag-red font-bold mt-4']">
              未对照数量：{{ unCompareNum }}个
            </p>
            <!-- <a-button type="primary" @click="saveCompare(false)">对照</a-button> -->
          </div>
        </div>
        <div style="width: 35%">
          <a-input
            v-model:value="formState.bm"
            placeholder="诊断代码/诊断名称/助记码"
            class="mb-2"
            allow-clear
            @press-enter="getRightDrugList()"
            @blur="getRightDrugList()"
          />
          <BaseTable
            column-code="001059"
            fill
            :data-source="drugData"
            bordered
            :pagination="{ total: page1.total, current: page1.current, pageSize: page1.pageSize }"
            @change="onPageChange1"
          >
            <template #bodyCell="{ column, index, record }">
              <template v-if="column.dataIndex === 'xh'">
                {{ index + 1 }}
              </template>
              <template v-if="column.dataIndex === 'action'">
                <a-button type="link" size="small" @click="saveCompare(false, record)">对照</a-button>
              </template>
            </template>
          </BaseTable>
        </div>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import { post } from '@/utils/request'

const loading = ref(false)
const formRef = ref(null)
const page = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const page1 = ref({ total: 0, current: 1, pageSize: getSystemConfig('DFFAULT_PAGE_SHOW_COUNT') || 10 })
const formState = ref({ nmiStatus: '' })
const selectLeftRow = ref({})
const tableData = ref([])
const drugData = ref([])
const selectRightRow = ref({})
const unCompareNum = ref(0)
const getData = async () => {
  loading.value = true
  const { code, data, total } = await post('/operation/opex/getCD10InfoList', {
    pageNum: page.value.current,
    pageSize: page.value.pageSize,
    pyjm: formState.value.pyjm,
    nmiStatus: formState.value.nmiStatus
  })
  loading.value = false
  if (code === 0) {
    page.value.total = total
    tableData.value = data || []
  }
}
const onHandleDeactivate = async (zddm, number) => {
  if (number === 0) {
    const { code, msg } = await post('/operation/opex/doEnableCD10', { zddm })
    if (code !== 0) return message.error(msg)
    message.success(msg)
  } else {
    const { code, msg } = await post('/operation/opex/doDisableCD10', { zddm })
    if (code !== 0) return message.error(msg)
    message.success(msg)
  }
  getData()
}

const getUnDataNum = async () => {
  const { code, data } = await post('/common/masterData/getNmiDictIcd10CountWzd')
  if (code === 0) {
    unCompareNum.value = data
  }
}
const getRightDrugList = async () => {
  const { code, data, total } = await post('/common/masterData/getNmiDictIcd10List', {
    pageNum: page1.value.current,
    pageSize: page1.value.pageSize,
    pyjm: formState.value.bm
  })
  if (code === 0) {
    page1.value.total = total
    drugData.value = data || []
  }
}
const onSelectRow = (select) => {
  selectLeftRow.value = select
  formState.value.bm = select.zdmc
  page1.value.current = 1
  getRightDrugList()
}
const saveCompare = async (flag, row) => {
  if (flag) selectLeftRow.value = row
  if (!selectLeftRow.value.zddm) return message.warning('请选中左边一项')
  const { code, msg } = await post('/common/masterData/doNmiDictIcd10Control', {
    zddm: selectLeftRow.value.zddm,
    icd10Code: row.icd10Code
  })
  if (code !== 0) return message.error(msg || '对照失败')
  message.success(msg || '对照成功')
  selectLeftRow.value = {}
  selectRightRow.value = {}
  formState.value.bm = ''
  getData()
  getUnDataNum()
  getRightDrugList()
}
const cancelCompare = async (record) => {
  const { code, msg } = await post('/common/masterData/doNmiDictIcd10QuashControl', {
    zddm: record.zddm
  })
  if (code !== 0) return message.error(msg || '撤销失败')
  message.success(msg || '撤销成功')
  getData()
  getUnDataNum()
}
const autoCompare = async () => {
  const { code, msg } = await post('/common/masterData/doNmiDictIcd10AutomaticControl')
  if (code !== 0) return message.error(msg || '自动对照失败')
  message.success(msg || '自动对照成功')
  selectLeftRow.value = {}
  selectRightRow.value = {}
  formState.value.bm = ''
  getData()
  getUnDataNum()
  getRightDrugList()
}
const onBlurInput = (record) => {
  if (record.change) {
    onSaveCode(record)
    record.change = false
  }
}
const onSaveCode = async (row) => {
  const { code, msg } = await post('/common/masterData/doNmiDictIcd10Control', {
    zddm: row.zddm,
    icd10Code: row.nmiIcd10Code,
    nmiIcd10Code: row.nmiIcd10Code,
    nmiIcd10Name: row.nmiIcd10Name
  })
  if (code !== 0) return message.error(msg || '保存失败')
  message.success(msg || '保存成功')
  getData()
}
const onPageChange = ({ current, pageSize }) => {
  page.value.current = current
  page.value.pageSize = pageSize
  getData()
}
const onPageChange1 = ({ current, pageSize }) => {
  page1.value.current = current
  page1.value.pageSize = pageSize
  getRightDrugList()
}
const handleReset = () => {
  formRef.value.resetFields()
  getData()
}
onMounted(() => {
  getData()
  getUnDataNum()
  getRightDrugList()
})
</script>
<style lang="less" scoped>
.ant-picker {
  width: 100%;
}

.table-row-select {
  background-color: red !important;
}
</style>
